<?php


namespace app\admin\controller;


use app\common\controller\Adminbase;
use think\Db;

class Teammoney extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $filter = input('filter');
            $where2[] = ['team_num','<>',-1];
            if ($filter) {
                $filter = json_decode($filter, true);

                if (isset($filter['nickname'])) {
                    $where2[] = ['m.nickname', 'like', '%' . $filter['nickname'] . '%'];
                }

                if (isset($filter['id'])) {
                    $where2[] = ['mf.id', '=', $filter['id']];
                }
                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where2[] = ['mf.create_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['is_send'])&&$filter['is_send']) {
                    $where2[] = ['mf.is_send', '=', $filter['is_send']];
                }
            }
            $_list = Db::name('team_money')
                ->alias('mf')
                ->join('member m', 'mf.member_id=m.id')
                ->field('mf.*,m.nickname,m.mobile')
                ->where($where2)
                ->order('create_time desc')
                ->page($page, $limit)
                ->select();
            $total = Db::name('team_money')
                ->alias('mf')
                ->join('member m', 'mf.member_id=m.id')
                ->where($where2)
                ->count();
            foreach ($_list as $k => $v) {
                $_list[$k]['team_num']+=1;
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            }
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        return $this->fetch('');
    }

    public function detail()
    {

        $finance_id = $this->request->param('id/d', 0);
        $data=Db::name('team_money')->find($finance_id);

        $data['create_time']=date('Y-m-d H:i:s',$data['create_time']);

        if ($data['send_time']) {
            $data['send_time'] = date('Y-m-d H:i:s', $data['send_time']);
        } else {
            $data['send_time'] = '待发送';
        }

        $this->assign("data", $data);
        $this->assign("is_detail", 1);
        return $this->fetch('teammoney/edit');
    }
    public function ckuan(){
        $id = $this->request->param('id/d', null);
        $finance_info=Db::name('team_money')->find($id);

        if ($finance_info['is_kuan']==1){
            $this->error('当前已转账！');
        }
        $update=array(
            'id'=>$id,
            'send_time'=>time(),
            'is_send'=>1
        );
        $result=Db::name('team_money')->update($update);
        if ($result){
            return ['code' => 200, 'data' => '', 'msg' => '操作成功'];
        }else{
            return ['code' => 100, 'data' => '', 'msg' => '操作失败'];
        }
    }
}